﻿


//所有弹层使用顶层layer
var _TopLayer = window.top.layer;
var _DLGTop = [];
const DLG = {
    Cancel: () => {
        var dlgIndex = window.top._DLGTop.length - 1;
        var dlgObj = window.top._DLGTop.pop();
        _TopLayer.close(dlgObj.index, () => {
            var layerDiv = window.top.document.getElementById(`dlg_layer_${dlgIndex}`);
            layerDiv && layerDiv.remove();
        });
    }
};

DLG.Show = function (width, height, url, caption, call, params, useIframe) {
    var w = width || "auto";
    if (typeof width == "number")
        w = width + "px";
    var h = height || "auto";
    if (typeof height == "number")
        h = height + "px";

    if (url.startsWith("http") && (useIframe || !url.startsWith(window.location.origin))) {//
        //通过顶层窗体的layer打开弹层，并且获取index和iframe添加到顶层窗体的数组
        var layerIndex = _TopLayer.open({
            type: 2,
            title: caption,
            closeBtn: 1,
            shadeClose: false,
            maxmin: true, //开启最大化最小化按钮
            area: [w, h],
            content: QB.GetEntFullPath(url),
            zIndex: _TopLayer.zIndex,
            success: function (layero, index, that) {
                //将index和call写入子页面，退出时使用
                //var ifwin = layero.find("iframe")[0].contentWindow;
                //ifwin._QYLayer = {
                //    index: index,
                //    callback: call,
                //};
            },
            cancel: function (index, layero, that) {
                DLG.Cancel();
            },
            end: function () {

            },
            min: function (layero, index, that) {
                // do something
                return false; // 阻止默认最小化
            }
        });

        window.top._DLGTop.push({ "index": layerIndex, "callback": call });
    } else {
        let layerDiv = document.createElement("div");
        layerDiv.id = `dlg_layer_${window.top._DLGTop.length}`;
        document.body.appendChild(layerDiv);
        var pageInfo = QB.GetPageJsUrlAndParams(url, true);
        QB.loadModule(layerDiv, pageInfo.url, pageInfo.params).then((newPage) => {
            if (params) {
                newPage.pageParams.push({ key: "params", value: params });
            }

            //通过顶层窗体的layer打开弹层，并且获取index和iframe添加到顶层窗体的数组
            var layerIndex = _TopLayer.open({
                type: 1,
                title: caption,
                closeBtn: 1,
                shadeClose: false,
                maxmin: true, //开启最大化最小化按钮
                area: [w, h],
                content: $(`#${layerDiv.id}`),
                zIndex: _TopLayer.zIndex,
                success: function (layero, index, that) {
                    //将index和call写入子页面，退出时使用
                    //var ifwin = layero.find("iframe")[0].contentWindow;
                    //ifwin._QYLayer = {
                    //    index: index,
                    //    callback: call,
                    //};
                },
                cancel: function (index, layero, that) {
                    DLG.Cancel();
                    return false;
                },
                end: function () {

                },
                min: function (layero, index, that) {
                    // do something
                    return false; // 阻止默认最小化
                }
            });

            window.top._DLGTop.push({ "index": layerIndex, "callback": call });
        });
    }

};

DLG.Quit = function (result) {
    var dlgIndex = window.top._DLGTop.length - 1;
    var dlgObj = window.top._DLGTop.pop();
    _TopLayer.close(dlgObj.index, () => {
        var layerDiv = window.top.document.getElementById(`dlg_layer_${dlgIndex}`);
        layerDiv.remove();
        if (typeof dlgObj.callback == "function")
            dlgObj.callback(result);
    });
};


DLG.show = DLG.Show;
DLG.quit = DLG.Quit;